﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;
using System.Web;


namespace StudentWeb.Api.Controllers
{
    public class BookController : ApiController
    {

        BookBO _bookBO = new BookBO();
        #region 图书信息 分页列表
        /// <summary>
        /// 图书信息 分页列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>


        

        [HttpPost]
        public IHttpActionResult GetBookList(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetBookListWhereC(model.condidate);
                var result = _bookBO.GetBookList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetBookListWhereC(dynamic qc)
        {
            //var wc = string.Empty;
            var wc = "BookID>0";
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.tabkey);
            if (!string.IsNullOrEmpty(tmp) && tmp != "all") { wc += $" and (BookState={tmp})"; }
            tmp = ConvertHelper.DynamicToString(qc.bookName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookAuthor);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookAuthor like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookNo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookNo like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookPublisher);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookPublisher like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookName);
            return wc;
        }
        #endregion
        #region 图书信息 单条记录
        /// <summary>
        /// 图书信息 单条记录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        //[ValidToken]
        public IHttpActionResult GetBookInfo(string token, int bookID)
        {
            try
            {
                var result = _bookBO.GetBookInfo(bookID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        [HttpPost]
        #region 查询全部数据的内容
        public IHttpActionResult GetBookList()
        {
            try
            {
                var result = _bookBO.GetBookList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 图书信息 保存
        /// <summary>
        /// 图书信息 保存
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult SaveBookInfo(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _bookBO.SaveBookInfo(model, user.TeacherDataID);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 图书信息 封面上传
        /// <summary>
        /// 图书信息 封面上传
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>

        public IHttpActionResult UploadFile()
        {
            var res = _bookBO.UploadFile(HttpContext.Current.Request.Files);
            return Json(res);
        }


        #endregion

        #region 图书修改 状态
        /// <summary>
        /// 图书修改 状态
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult SetBookState(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int bookID = model.id;
            int bookState = model.state;
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _bookBO.SetBookState(bookID, bookState);
                return Json(result); 
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }

}